package com.binarycat.dynamiclimit;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.binarycat.dynamiclimit.bean.DynamicLimitData;
import com.binarycat.dynamiclimit.bean.DynamicLimiterConfig;
import com.binarycat.dynamiclimit.collector.AbstractLimitDataCollector;
import com.binarycat.dynamiclimit.collector.DefaultLimitDataCollector;
import com.binarycat.dynamiclimit.initialization.DynamicLimitInitialization;
import com.binarycat.dynamiclimit.limiter.DynamicLimiter;
import com.binarycat.dynamiclimit.limiter.policy.AbstractLimitPolicy;

/**
 * 动态限流管理器
 * 
 * @author jjb
 * @date 2018/08/06
 */
public final class DynamicLimiterManager
    implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private DynamicLimiterConfig dynamicLimiterConfig;

    private DynamicLimitData limitData = new DynamicLimitData();

    private AbstractLimitDataCollector limitDataCollector = new DefaultLimitDataCollector();

    private List<AbstractLimitPolicy> dynamicLimitPolicyList = new ArrayList<AbstractLimitPolicy>();

    private DynamicLimiter dynamicLimiter = new DynamicLimiter();

    private DynamicLimitInitialization init = new DynamicLimitInitialization();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 当spring容器初始化完成后就会执行该方法。
        ApplicationContext applicationContext = event.getApplicationContext();
        init.initLimitInfo(applicationContext, this);
    }

    public DynamicLimiterConfig getDynamicLimiterConfig() {
        return dynamicLimiterConfig;
    }

    public void setDynamicLimiterConfig(DynamicLimiterConfig dynamicLimiterConfig) {
        this.dynamicLimiterConfig = dynamicLimiterConfig;
    }

    public DynamicLimitData getLimitData() {
        return limitData;
    }

    public void setLimitData(DynamicLimitData limitData) {
        this.limitData = limitData;
    }

    public AbstractLimitDataCollector getLimitDataCollector() {
        return limitDataCollector;
    }

    public void setLimitDataCollector(AbstractLimitDataCollector limitDataCollector) {
        this.limitDataCollector = limitDataCollector;
    }

    public List<AbstractLimitPolicy> getDynamicLimitPolicyList() {
        return dynamicLimitPolicyList;
    }

    public DynamicLimiter getDynamicLimiter() {
        return dynamicLimiter;
    }

    public void setDynamicLimitor(DynamicLimiter dynamicLimiter) {
        this.dynamicLimiter = dynamicLimiter;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
